################################################################
################################################################
######## Value Polarization and Affective Polarization #########
################################################################
################################################################

library(lattice)
library(psych)
library(MASS)
library(foreign)
library(mokken)
library(gridExtra)
library(car)
library(effects)

# SET WORKING DIRECTORY 

####
## Figure 1 in main text
####

mfxdata <- read.csv("valuepolmfx.csv", header=TRUE)
as.data.frame(mfxdata)

pdf("mfxplot.pdf")

xyplot(estimate ~ as.factor(year) | item, data = mfxdata,
       xlab = "Year",
       ylab = "MFX of Value Polarization",
       aspect = 1,
       index.cond=list(c(3, 2, 1)),
       layout=c(3, 1),
       scales =
         list(x = list(labels=c("'88", "'92", "'96", "'00", "'04", "'08", "'12", "'16"))),
       groups = group,
       col = c("black", "darkgrey", "darkgrey"),
       type = "l",  
       lty = c(1, 2, 2)
)

dev.off()

####
## Figures A1-A8 of Supplemental Appendix
####

values.data <- read.dta("values-polarization2.dta")
head(values.data)

valuequestions <- na.omit(values.data[c(84,86,87,89,90,91,92,93)])
head(valuequestions)
sumrate <- as.data.frame(scale(valuequestions))

reliab.sumrate <- alpha(valuequestions)
reliab.sumrate
reliab.sumrate$alpha.drop

scale <- apply(valuequestions, MARGIN = 1, FUN = mean)
summary(scale)
sd(scale)

histogram(~scale, 
          aspect = 1,
          xlab = "Eight-Item Summated Rating Scale"
)

restscores <- matrix(nrow = nrow(valuequestions), ncol = ncol(valuequestions))
names <- rep("rest", times = ncol(valuequestions))
for (j in 1:ncol(valuequestions)) {
  restscores[, j] <- apply(valuequestions[, -j], MARGIN = 1, FUN = mean)
  names[j] <- paste(names[j], colnames(valuequestions)[j], sep = ".")
}
colnames(restscores) <- names

pdf("equalopp.pdf")

set.seed(1234)
xyplot(valuequestions[,1] ~ restscores[,1],
       aspect = 1,
       ylab = "Equalopp",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


pdf("equalchance.pdf")

set.seed(1234)
xyplot(valuequestions[,2] ~ restscores[,2],
       aspect = 1,
       ylab = "Equalchance",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


pdf("lessequal.pdf")

set.seed(1234)
xyplot(valuequestions[,3] ~ restscores[,3],
       aspect = 1,
       ylab = "Lessequal",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


pdf("fewer.pdf")

set.seed(1234)
xyplot(valuequestions[,4] ~ restscores[,4],
       aspect = 1,
       ylab = "Fewer",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


pdf("changing.pdf")

set.seed(1234)
xyplot(valuequestions[,5] ~ restscores[,5],
       aspect = 1,
       ylab = "Changing",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


pdf("Lifesytles.pdf")

set.seed(1234)
xyplot(valuequestions[,6] ~ restscores[,6],
       aspect = 1,
       ylab = "Lifestyles",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


pdf("standards.pdf")

set.seed(1234)
xyplot(valuequestions[,7] ~ restscores[,7],
       aspect = 1,
       ylab = "Standards",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


pdf("family.pdf")

set.seed(1234)
xyplot(valuequestions[,8] ~ restscores[,8],
       aspect = 1,
       ylab = "Family",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()

####
## Figure A9 in Supplemental Appendix
####

pdf("sumrate.pdf")

histogram(~values,
          data = values.data,
          aspect = 1,
          xlab = "Eight-item Summated Rating Scale"
          )

dev.off()

####
## Figure A10 in Supplemental Appendix
####

values.data$timedum <- factor(values.data$timedum, labels = c("1988", "2016"))

pdf("densityplot.pdf")

densityplot(~values | as.factor(timedum), data = values.data,
            group = as.factor(rep),
            xlab = "Values Scale Scores",
            aspect = 1,
            col = c("blue", "red"),
            plot.points = FALSE,
            key=list(columns=1,rectangles=list(col=c("red","blue"), alpha=.8),
                     text=list(c("Republicans", "Democrats")))
)

dev.off()

####
## Figure A11 of Supplemental Appendix
####

mfxdata2 <- read.csv("mfx-elitepol.csv", header=TRUE)
as.data.frame(mfxdata2)

pdf("mfx-nominate.pdf")

xyplot(estimate ~ level | item, data = mfxdata2,
       xlab = "Level of Elite Polarization",
       ylab = "MFX of Value Polarization",
       aspect = 1,
       index.cond=list(c(3, 2, 1)),
       layout=c(3, 1),
       groups = group,
       col = c("black", "darkgrey", "darkgrey"),
       type = "l",  
       lty = c(1, 2, 2)
)

dev.off()



